package com.ycy.controller.employee;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.ibatis.annotations.Param;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ycy.pojo.Employee;
import com.ycy.pojo.Role;
import com.ycy.service.Role.RoleService;
import com.ycy.service.employee.EmployService;



@Controller
public class EmployController {
	
	@Autowired
	EmployService employservice;
	@Autowired 
	private RoleService roleService;
	
	@RequestMapping("/employee_list")
	public String employeeList(HttpServletRequest request,Model m,HttpSession session){
		Employee employee1=(Employee)session.getAttribute("Me");
		Role role=roleService.findByName(employee1.getName());
		if(employee1.getSection()!=null&& role.getRole().equals("管理员")){
		List<Employee> employees= employservice.findBySection(employee1.getSection());
		session.setAttribute("Employees",employees);
		}else if(role.getRole().equals("超级管理员")){
			List<Employee> employees=employservice.findAll();
			session.setAttribute("Employees",employees);
		}else{
			Employee employee=employservice.find(employee1.getId());
			session.setAttribute("Employees", employee);
		}
		return "employ_list";
	}
	
	@RequestMapping(value = "/employee_edit")
	public String employEdit(int id,Model model){
		
		    Employee employee2= employservice.find(id);
			model.addAttribute("Employee", employee2);
			return "employ_edit";
		}
		
		@RequestMapping("/do_employ_edit")
		@ResponseBody
		public String employDoEdit(Employee employee){
			
			int num=employservice.update(employee);
			if(num>0) {
				return "ok";
						}
			else{
				return "fail";
			}
		}
		
		@RequestMapping(value = "/employ_add")
		public String employee(){
			
				return "employ_add";
			}
			
			@RequestMapping("/do_employ_add")
			@ResponseBody
			public String employDoAdd(Employee employee){
				
				int num=employservice.add(employee);
				if(num>0) {
					return "ok";
							}
				else{
					return "fail";
				}
			}
			@RequestMapping("/employ_stop")
			@ResponseBody
			public String userStop(@Param("id") int id){
				
				int i=employservice.stop(id);
				if(i==1){
					return "ok";
				}else{
					return "fail";
				}
			}
			@RequestMapping("/employ_start")
			@ResponseBody
			public String userStart(@Param("id") int id){
				int i=employservice.start(id);
				if(i==1){
					return "ok";
				}else{
					return "fail";
				}
				
			}
			@RequestMapping("/employ_del")
			@ResponseBody
			public String employDel(@Param("id") int id){
				
				int i=employservice.del(id);
				if(i==1){
					return "ok";
				}else{
					return "fail";
				}
			}
			
			@RequestMapping("/administrator_list")
			public String administratorList(HttpServletRequest request,Model m,HttpSession session){
				Employee employee1=(Employee)session.getAttribute("Me");
				Role role=roleService.findByName(employee1.getName());
				if(role.getRole().equals("超级管理员")){
					List<Employee> employees= employservice.findAdmin();
					session.setAttribute("administrators",employees);
				}else{
					session.setAttribute("administrators",null);
				}
				
				return "administrator_list";
			}
			@RequestMapping(value = "/employ_me")
			public String employMe(Model model){
					return "employ_me";
				}
				
				@RequestMapping("/do_employ_me")
				@ResponseBody
				public String employDoMe(Employee employee){
					System.out.println(employee);
					int num=employservice.edit(employee);
					if(num>0) {
						return "ok";
								}
					else{
						return "fail";
					}
				}
}
